update.ts ➔ update   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 11
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 11
rs 8
c 0
b 0
f 0
cc 7
1
import { WriteTags } from "../types/Tags"
2
import { Options } from "../types/Options"
3
import { isFunction, isString } from "../util"
4
import { read } from "./read"
5
import { updateTags } from '../updateTags'
6
import { write, WriteCallback } from "./write"
7
8
/**
9
 * Updates ID3-Tags from the given buffer.
10
 *
11
 * @public
12
 */
13
 export function update(
14
    tags: WriteTags,
15
    buffer: Buffer,
16
    options?: Options
17
): Buffer
18
19
/**
20
 * Updates ID3-Tags synchronously in the specified file.
21
 *
22
 * @public
23
 */
24
 export function update(
25
    tags: WriteTags,
26
    filepath: string,
27
    options?: Options
28
): true | Error
29
30
/**
31
 * Updates ID3-Tags asynchronously in the specified file.
32
 *
33
 * @public
34
 */
35
 export function update(
36
    tags: WriteTags,
37
    filebuffer: string | Buffer,
38
    callback: WriteCallback
39
): void
40
41
/**
42
 * Updates ID3-Tags asynchronously from the given buffer or specified file.
43
 *
44
 * @public
45
 */
46
 export function update(
47
    tags: WriteTags,
48
    filebuffer: string | Buffer,
49
    options: Options,
50
    callback: WriteCallback
51
): void
52
53
export function update(
54
    tags: WriteTags,
55
    filebuffer: string | Buffer,
56
    optionsOrCallback?: Options | WriteCallback,
57
    callback?: WriteCallback
58
): Buffer | true | Error | void {
59
    const options: Options =
60
        (isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {}
61
    callback =
62
        isFunction(optionsOrCallback) ? optionsOrCallback : callback
63
64
    const currentTags = read(filebuffer, options)
65
    const updatedTags = updateTags(tags, currentTags)
66
    if (isFunction(callback)) {
67
        return write(updatedTags, filebuffer, callback)
68
    }
69
    if (isString(filebuffer)) {
70
        return write(updatedTags, filebuffer)
71
    }
72
    return write(updatedTags, filebuffer)
73
}
74